/*
 * Copyright 2003-2005 (c) Live Media Pty Ltd. <argot@einet.com.au> 
 *
 * This software is licensed under the Argot Public License 
 * which may be found in the file LICENSE distributed 
 * with this software.
 *
 * More information about this license can be found at
 * http://www.einet.com.au/License
 * 
 * The Developer of this software is Live Media Pty Ltd,
 * PO Box 4591, Melbourne 3001, Australia.  The license is subject 
 * to the law of Victoria, Australia, and subject to exclusive 
 * jurisdiction of the Victorian courts.
 */

#include "Types.h"

#include "TypeInputStream.h"

/* TODO remove read_byte */

/* Create a input structure */
void type_input_init( type_input_stream * input, type_map * map, argot_read ar, void * data )
{
	input->map = map;
	input->read_buffer = ar;
	input->data = data;
	type_stack_init( &input->stack );
	type_stack_push( &input->stack );
}

/* Destroy a input structure */
void type_input_destroy( type_input_stream * tis )
{
}

/* Set an error on the stream */
void type_input_set_error( type_input_stream * tis, int rc )
{
	tis->error = rc;
}

/* Get/Check error on stream. Returns NULL if no error */
int type_input_get_error( type_input_stream * tis )
{
	
	return tis->error;
}

void type_input_set_type_map( type_input_stream * tis, type_map * map )
{
	tis->map = map;
}

/* Read an object using the system id from the stream */
int type_input_read_object( type_input_stream * tis,int id )
{
	typeReader reader = type_map_get_reader( tis->map, id );
	type_element * element = type_map_get_structure( tis->map, id );
	return reader( tis, element );
}

/* Read an object using the object name */
int type_input_read_named_object( type_input_stream * tis, type_name * name )
{

	int id = type_map_get_id( tis->map, name );
	if ( id == TYPE_NOT_DEFINED )
	{
		type_input_set_error( tis, TYPE_NOT_DEFINED );
		return id;
	}
		
	typeReader reader = type_map_get_reader( tis->map, id );
	type_element * element = type_map_get_structure( tis->map, id );
	return reader( tis, element );
}
